/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.client.OnlineStorageService;
import com.nubotech.gwt.oss.client.OnlineStorageServiceFactory;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;
import com.nubotech.gwt.oss.client.auth.Credential;

/**
 *
 * @author jonnakkerud
 */
public class CreateBucketPanel extends Composite {
    private VerticalPanel main = new VerticalPanel();
    private TextBox tb;
    private BrowserView browserView;
   // private DeckPanel deck;
   // private FileView fileView;
    
    public CreateBucketPanel(BrowserView browserView) {
        initWidget(main);
        this.browserView = browserView;
        //this.deck = deck;
        //this.fileView = fileView;
        initGui();
    }
        
    private void initGui() {
        HorizontalPanel hp = new HorizontalPanel();
        hp.setSpacing(4);

        final ListBox lb = new ListBox();
        lb.addItem("Private");
        lb.addItem("Public Read");
        lb.addItem("Public Read/Write");
        lb.addItem("Authenticated Read");
        
        tb = new TextBox();
        Button btn = new Button("Create");
        btn.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                createBucket(tb.getText(), lb.getSelectedIndex());
            }
        });
        hp.add(tb);
        hp.add(btn);
        main.add(hp);
        
        main.add(lb);
        
    }

    private void createBucket(String bucketName, int acl) {
        Credential c = AuthenticationManager.instance().getLogin();
        if (c != null) {
            OnlineStorageService oss = OnlineStorageServiceFactory.getService(c);
            oss.createBucket(bucketName, acl);
            DeferredCommand.addCommand(new Command() {
                public void execute() {
                    tb.setText("");
                    //deck.showWidget(0);
                    //fileView.refresh();
                    browserView.refresh();
                }
            });
        }
        
    }
    
    //protected void onLoad() {
    //    initGui();
    //}
    
}
